home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Xconq 7.0d37 / source / x11 / xprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-26  |  11.3 KB  |  470 lines  |  [TEXT/KAHL]

  1. /* Commands for the X11 interface to Xconq.
  2.    Copyright (C) 1994, 1995 Massimo Campostrini & Stanley T. Shebs.
  3.  
  4. Xconq is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.  See the file COPYING.  */
  8.  
  9. #include "conq.h"
  10. #include "xconq.h"
  11.  
  12. /* Xconq 7.0 printing options front-end.
  13.    Version 0.0  May 1994.  
  14.    By Massimo Campostrini <campo@sunthpi3.difi.unipi.it>.
  15. */
  16.  
  17. #define N_COMMAND  3
  18. #define N_CHOICE   1
  19. #define N_BUTTON   (N_COMMAND+N_CHOICE)
  20. #define N_TOGGLE   6
  21. #define N_DIALOG  12
  22. #define N_DIMEN_D 10 
  23. #define N_WIDGET (N_BUTTON+N_TOGGLE+N_DIALOG)
  24.  
  25. static int choi[N_CHOICE];
  26. static int flag[N_TOGGLE];
  27. static double parm[N_DIALOG];
  28. static int i_metric;
  29. static Side *ps_side;
  30.  
  31. static PrintParameters *ps_pp = NULL;
  32.  
  33. typedef struct a_widgetId {
  34.   String      name;
  35.   int         newline;
  36. } WidgetId;
  37.  
  38. typedef struct a_choice {
  39.   int number;
  40.   String legends[5];
  41. } Choice;
  42.  
  43. Choice choices[N_CHOICE] = {
  44.   {3, 
  45.      {"no names",
  46.       "interesting names",
  47.       "all names",
  48.       NULL, NULL}
  49.  }
  50. };
  51.  
  52. void print_parms PROTO ((void));
  53. void print_ok PROTO ((void));
  54. void print_skip PROTO ((void));
  55. void open_print_help PROTO ((void));
  56. static void done_print_help ();
  57.  
  58. int w_comm_ind PROTO ((Widget w));
  59. void update_values PROTO ((void));
  60. void hilight_label PROTO ((void));
  61. void do_dialog PROTO ((Widget wdgt, XEvent *event, String *params, Cardinal *num_params));
  62. void handle_print_setup_button ();
  63. void handle_metric_convert ();
  64. void read_all PROTO ((void));
  65.  
  66. static XtActionsRec xcps_actions[] = {
  67.   { "DoButton", handle_print_setup_button },
  68.   { "DoMetric", handle_metric_convert },
  69.   { "DoDialog", do_dialog },
  70.   { "DoneHelp", done_print_help },
  71. };
  72.  
  73. /* these should go somewhere else.  Massimo 
  74. static String fallback_resources[] = {
  75.   "*Dialog*value: 0",
  76.   "*Dialog*value.translations: #override \\n <Key>Return: DoDialog()",
  77.   "*help.Label: \\ help ",
  78.   "*print.Label:\\ print ",
  79.   "*quit.Label: \\ quit ",
  80.   "*names.label: interesting names",
  81.   "*cellSize.label: cell size",
  82.   "*gridSize.label: grid thick.",
  83.   "*pageWidth.label: page width",
  84.   "*pageHeight.label: page height",
  85.   "*topMargin.label: top margin",
  86.   "*bottomMargin.label: bottom margin",
  87.   "*leftMargin.label: left margin",
  88.   "*rightMargin.label: right margin",
  89.   "*terrainGray.label: terrain gray",
  90.   "*enemyGray.label: enemy gray",
  91.   "*borderWidth.label: border thick.",
  92.   "*connWidth.label: conn thick.",
  93.   "*PrintSetupHelp.title: print setup help",
  94.   "*PrintSetupHelp*baseTranslations: #override \
  95.     <Key>Return: DoneHelp()\\n\
  96.     <Key>Escape: DoneHelp()\\n\
  97.     <Btn1Down>:  DoneHelp()",
  98.   "*PrintSetupHelp*helpDone.Label: \\ done ",
  99.   NULL,
  100. }; */
  101.  
  102. Widget w_shell = NULL, w_comm[N_WIDGET], w_help_shell=NULL;
  103.  
  104. WidgetId w_table[N_WIDGET] = {
  105.   {"help",         1 },
  106.   {"print",        0 },
  107.   {"quit",         0 },
  108.   {"names",        1 },
  109.   {"coord",        0 },
  110.   {"dither",       0 },
  111.   {"double",       1 },
  112.   {"features",     0 },
  113.   {"summary",      0 },
  114.   {"metric",       0 },
  115.   {"cellSize",     1 },
  116.   {"gridSize",     0 },
  117.   {"borderWidth",  1 },
  118.   {"connWidth",    0 },
  119.   {"pageWidth",    1 },
  120.   {"pageHeight",   0 },
  121.   {"topMargin",    1 },
  122.   {"bottomMargin", 0 },
  123.   {"leftMargin",   1 },
  124.   {"rightMargin",  0 },
  125.   {"terrainGray",  1 },
  126.   {"enemyGray",    0 },
  127. };
  128.  
  129. static String helpText =
  130. "Modify values, then press \"print\" (or \"quit\" to skip printing).\n\
  131.  \n\
  132. Multiple choices (click to cycle)\n\
  133. o    print unit names:  no names, interesting names, all names.\n\
  134.  \n\
  135. Binary options (dark background = set)\n\
  136. o    coord:    print coordinates of map corners\n\
  137. o    dither:   use dithering to print terrain gray\n\
  138. o    double:   map a pixel into 2x2 pixels wile dithering\n\
  139. o    features: print geographical features\n\
  140. o    summary:  print summary of units in cell\n\
  141. o    metric:   print distances in centimetres (otherwise in inches)\n\
  142.  \n\
  143. Numerical options (accept floating-point values)\n\
  144. o    cell size:  printed size of hexagonal cells\n\
  145. o    grid thick.:  thickness of cell grid (0 to suppress)\n\
  146. o    border/conn thick.:  thickness of borders and connections\n\
  147. o    page width/height:  dimension of sheets to print on\n\
  148. o    top/bottom/left/right margin:  margins of the region where\n\
  149. cells are printed; labels and titles are printed in the margins\n\
  150. o    border/connection thickness\n\
  151. o    terrain gray:  from 0 (black) to 1 (white)\n\
  152. o    enemy gray:    from 0 (black) to 1 (white), [not implemented]";
  153.  
  154. int
  155. popup_print_setup_dialog(side, pspp)
  156. Side *side;
  157. PrintParameters *pspp;
  158. {
  159.   int i;
  160.   Widget w_form, w_left, w_up, w_oldup;
  161.   WidgetClass class;
  162.   ps_side = side;
  163.  
  164.   XtAppAddActions(thisapp, xcps_actions, XtNumber(xcps_actions));
  165.  
  166.   ps_pp = pspp;
  167.  
  168.   if (!w_shell) {
  169.     w_shell =
  170.       XtVaCreatePopupShell("PrintSetup", topLevelShellWidgetClass,
  171.                side->ui->shell, NULL);
  172.   
  173.     w_form = XtVaCreateManagedWidget ("form", formWidgetClass, w_shell, NULL);
  174.     w_left = w_up = w_oldup = NULL;
  175.   
  176.     for (i = 0; i < N_WIDGET; i++) {
  177.       if (i < N_BUTTON)
  178.     class = commandWidgetClass;
  179.       else if (i < N_BUTTON + N_TOGGLE)
  180.     class = toggleWidgetClass;
  181.       else
  182.     class = dialogWidgetClass;
  183.     
  184.       w_comm[i] = XtVaCreateManagedWidget (w_table[i].name, class, w_form,
  185.                        NULL);
  186.       if (w_table[i].newline) {
  187.     w_oldup = w_up;
  188.     w_up = w_comm[i];
  189.     w_left = NULL;
  190.       }
  191.       XtVaSetValues(w_comm[i],
  192.              XtNfromVert, w_oldup,
  193.              XtNfromHoriz, w_left,
  194.              NULL);
  195.       w_left = w_comm[i];
  196.     
  197.       if (strcmp(w_table[i].name, "metric") == 0) {
  198.     XtAddCallback (w_comm[i], XtNcallback, handle_metric_convert, NULL);
  199.     i_metric = i;
  200.     }
  201.       /* yes, we really need to cast commandWidgetClass into WidgetClass */
  202.       if (class == (WidgetClass) commandWidgetClass) {
  203.     XtAddCallback (w_comm[i], XtNcallback,
  204.                handle_print_setup_button, NULL);
  205.       }
  206.     }
  207.   
  208.     XtRealizeWidget (w_shell);
  209.   }    
  210.  
  211.   /* unelegant... */
  212.   choi[ 0] = ps_pp->names;
  213.   flag[ 0] = ps_pp->corner_coord;
  214.   flag[ 1] = ps_pp->terrain_dither;
  215.   flag[ 2] = ps_pp->terrain_double;
  216.   flag[ 3] = ps_pp->features;
  217.   flag[ 4] = ps_pp->cell_summary;
  218.   flag[ 5] = ps_pp->cm;
  219.   parm[ 0] = ps_pp->cell_size;
  220.   parm[ 1] = ps_pp->cell_grid_width;
  221.   parm[ 2] = ps_pp->border_width;
  222.   parm[ 3] = ps_pp->connection_width;
  223.   parm[ 4] = ps_pp->page_width;
  224.   parm[ 5] = ps_pp->page_height;
  225.   parm[ 6] = ps_pp->top_margin;
  226.   parm[ 7] = ps_pp->bottom_margin;
  227.   parm[ 8] = ps_pp->left_margin;
  228.   parm[ 9] = ps_pp->right_margin;
  229.   parm[10] = ps_pp->terrain_gray;
  230.   parm[11] = ps_pp->enemy_gray;
  231.   update_values();
  232.  
  233.   XtPopup(w_shell, XtGrabNone);
  234.  
  235.   return 0;
  236. }
  237.  
  238. void 
  239. print_ok()
  240. {
  241.   double conv;
  242.  
  243.   /* unelegant... */
  244.   ps_pp->names =        choi[ 0];
  245.   ps_pp->corner_coord =        flag[ 0];
  246.   ps_pp->terrain_dither =    flag[ 1];
  247.   ps_pp->terrain_double =    flag[ 2];
  248.   ps_pp->features =        flag[ 3];
  249.   ps_pp->cell_summary =        flag[ 4];
  250.   ps_pp->cm =            flag[ 5];
  251.   ps_pp->cell_size =        parm[ 0];
  252.   ps_pp->cell_grid_width =    parm[ 1];
  253.   ps_pp->border_width =        parm[ 2];
  254.   ps_pp->connection_width =    parm[ 3];
  255.   ps_pp->page_width =        parm[ 4];
  256.   ps_pp->page_height =        parm[ 5];
  257.   ps_pp->top_margin =        parm[ 6];
  258.   ps_pp->bottom_margin =    parm[ 7];
  259.   ps_pp->left_margin =        parm[ 8];
  260.   ps_pp->right_margin =        parm[ 9];
  261.   ps_pp->terrain_gray =        parm[10];
  262.   ps_pp->enemy_gray =        parm[11];
  263.   if (w_shell) {
  264.     XtPopdown (w_shell);
  265.   }
  266.   if (w_help_shell) {
  267.     XtPopdown (w_help_shell);
  268.   }
  269.   /* convert from cm or in */
  270.   if (ps_pp->cm) {
  271.     conv = 72/2.54;
  272.   } else {
  273.     conv = 72;
  274.   }
  275.   ps_pp->cell_size *= conv;
  276.   ps_pp->cell_grid_width *= conv;
  277.   ps_pp->border_width *= conv;
  278.   ps_pp->connection_width *= conv;
  279.   ps_pp->page_width *= conv;
  280.   ps_pp->page_height *= conv;
  281.   ps_pp->top_margin *= conv;
  282.   ps_pp->bottom_margin *= conv;
  283.   ps_pp->left_margin *= conv;
  284.   ps_pp->right_margin *= conv;
  285.  
  286.   /* dump view */    
  287.   notify(ps_side, "dumping view to file \"view.xconq\" ...");
  288.   flush_output(ps_side);
  289.   dump_ps_view(ps_side, ps_pp, "view.xconq");
  290.   notify(ps_side, "... done.");
  291.  
  292. }
  293.  
  294. void 
  295. print_skip()
  296. {
  297.     double conv;
  298.  
  299.     if (w_shell)
  300.       XtPopdown (w_shell);
  301.     if (w_help_shell)
  302.       XtPopdown (w_help_shell);
  303.  
  304.     /* Convert from cm or in. */
  305.     conv = 72;
  306.     if (ps_pp->cm)
  307.       conv /= 2.54;
  308.  
  309.     ps_pp->cell_size *= conv;
  310.     ps_pp->cell_grid_width *= conv;
  311.     ps_pp->border_width *= conv;
  312.     ps_pp->connection_width *= conv;
  313.     ps_pp->page_width *= conv;
  314.     ps_pp->page_height *= conv;
  315.     ps_pp->top_margin *= conv;
  316.     ps_pp->bottom_margin *= conv;
  317.     ps_pp->left_margin *= conv;
  318.     ps_pp->right_margin *= conv;
  319. }
  320.  
  321. void 
  322. update_values()
  323. {
  324.     char buffer[80];
  325.     int i;
  326.     char flg;
  327.     Arg args[1];
  328.  
  329.     for (i = 0; i < N_CHOICE; i++) {
  330.     XtSetArg(args[0], XtNlabel, (XtArgVal) choices[i].legends[choi[i]]);
  331.     XtSetValues(w_comm[i + N_COMMAND], args, 1);
  332.     }
  333.     for (i = 0; i < N_TOGGLE; i++) {
  334.     flg = flag[i];
  335.     XtSetArg(args[0], XtNstate, (XtArgVal) flg);
  336.     XtSetValues(w_comm[i + N_BUTTON], args, 1);
  337.     }
  338.     for (i = 0; i < N_DIALOG; i++) {
  339.     sprintf(buffer, "%.11g", parm[i]);
  340.     XtSetArg(args[0], XtNvalue, (XtArgVal) buffer);
  341.     XtSetValues(w_comm[i + N_BUTTON + N_TOGGLE], args, 1);
  342.     }
  343. }
  344.  
  345. int
  346. w_comm_ind(w)
  347. Widget w;
  348. {
  349.     int i;
  350.  
  351.     for (i = 0; i <= N_WIDGET; i++) {
  352.     if (w == w_comm[i])
  353.       return i;
  354.     }
  355.     fprintf(stderr, "w_comm_ind: widget not found\n");
  356.     return -1;
  357. }
  358.  
  359. void
  360. do_dialog(w, event, params, numparms)
  361. Widget w;
  362. XEvent *event;
  363. String *params;
  364. Cardinal *numparms;
  365. {
  366.     read_all();
  367.     update_values();
  368. }
  369.  
  370. void 
  371. read_all()
  372. {
  373.     String string;
  374.     int i;
  375.     double result;
  376.     char flg;
  377.     Arg args[1];
  378.  
  379.     for (i = 0; i < N_TOGGLE; i++) {
  380.     XtSetArg(args[0], XtNstate, (XtArgVal) &flg);
  381.     XtGetValues(w_comm[i+N_BUTTON], args, 1);
  382.     flag[i] = flg;
  383.     }
  384.     for (i = 0; i < N_DIALOG; i++) {
  385.     string = XawDialogGetValueString(w_comm[i+N_BUTTON+N_TOGGLE]);
  386.     sscanf(string, "%lf", &result);
  387.     parm[i] = result;
  388.     }
  389. }
  390.  
  391. void 
  392. handle_print_setup_button(w)
  393. Widget w;
  394. {
  395.     int i = w_comm_ind(w);
  396.  
  397.     read_all();
  398.  
  399.     if (i == 0) {
  400.     open_print_help(); 
  401.     } else if (i == 1) {
  402.     print_ok();
  403.     } else if (i == 2) {
  404.         print_skip();
  405.     } else if (i >= N_COMMAND) {
  406.     i -= N_COMMAND;
  407.     choi[i] = (choi[i] + 1) % choices[i].number;
  408.     }
  409.     update_values();
  410. }
  411.  
  412. /* Convert in to cm, or vice versa. */
  413.  
  414. void 
  415. handle_metric_convert (w)
  416. Widget w;
  417. {
  418.     char flg;
  419.     int i;
  420.     double conv;
  421.     Arg args[1];
  422.  
  423.     XtSetArg(args[0], XtNstate, (XtArgVal) &flg);
  424.     XtGetValues(w_comm[i_metric], args, 1);
  425.     conv = flg ? 2.54 : (1 / 2.54);
  426.     read_all();
  427.     for (i = 0; i < N_DIMEN_D; i++) {
  428.     parm[i] *= conv;
  429.     }
  430.     update_values();
  431. }
  432.  
  433. static void 
  434. done_print_help(w, client_data, call_data)
  435. Widget w;
  436. XtPointer client_data;
  437. XtPointer call_data;
  438. {
  439.     if (w_help_shell)
  440.       XtPopdown(w_help_shell);
  441. }
  442.  
  443. void
  444. open_print_help()
  445. {
  446.     Widget w_form, w_done, w_text;
  447.  
  448.     if (!w_help_shell) {
  449.     /* If first time, create it. */
  450.     w_help_shell =
  451.       XtVaCreatePopupShell("PrintSetupHelp", topLevelShellWidgetClass,
  452.                    ps_side->ui->shell,
  453.                    NULL);
  454.     w_form =
  455.       XtVaCreateManagedWidget("helpForm", formWidgetClass, w_help_shell,
  456.                   NULL);
  457.     w_text =
  458.       XtVaCreateManagedWidget("helpText", labelWidgetClass, w_form,
  459.                   XtNlabel, helpText,
  460.                   NULL);
  461.     w_done =
  462.       XtVaCreateManagedWidget("helpDone", commandWidgetClass, w_form,
  463.                   XtNfromVert, w_text,
  464.                   NULL);
  465.     XtAddCallback(w_done, XtNcallback, done_print_help, NULL);
  466.     }
  467.     XtPopup(w_help_shell, XtGrabNone);
  468. }
  469.  
  470.